package myaop.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

@Aspect //代表当前这个类是一个被Spring管理的AOP切面类
public class MyAnnoAopLogger {
    //定义切点
    @Pointcut("execution(* myaop.service.UserService.*(..))")
    public void pointcut(){}

    @Before("pointcut()")
    public void before(JoinPoint jp){
        System.out.println(jp.getTarget()+"."+jp.getSignature().getName()+"执行了前置增强");
    }
    @After("pointcut()")
    public void after(JoinPoint jp){
        System.out.println(jp.getTarget()+"."+jp.getSignature().getName()+"执行了最终增强");
    }
// 配置 <aop:after-returning method="myAfter" pointcut-ref="pointcut2" returning="obj"/>
    @AfterReturning(pointcut = "pointcut()",returning = "result")
    public void afterReturning(JoinPoint jp,Object result){
        System.out.println(jp.getTarget()+"."+jp.getSignature().getName()+"执行了后置增强");
    }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint jp) throws Throwable {
        try {
            System.out.println(jp.getTarget()+"."+jp.getSignature().getName()+"执行了环绕增强");
            Object object= jp.proceed();
            return object;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return null;
        }finally {
            System.out.println("环绕增强执行完毕");
        }

    }
    @AfterThrowing(pointcut = "pointcut()",throwing = "ex")
    public void afterThrowing(JoinPoint jp,Exception ex){
        System.out.println(jp.getTarget()+"."+jp.getSignature().getName()+"执行了异常增强");
    }


}
